# ui/components.py - UI组件
import tkinter as tk
from tkinter import ttk, scrolledtext

def create_main_ui(root):
    """创建主界面UI组件"""
    # 创建主框架
    main_frame = ttk.Frame(root)
    main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
    
    # 左侧文件列表框架
    left_frame = ttk.Frame(main_frame, width=250)
    left_frame.pack(side=tk.LEFT, fill=tk.Y, padx=(0, 10))
    left_frame.pack_propagate(False)
    
    # 搜索框
    search_frame = ttk.Frame(left_frame)
    search_frame.pack(fill=tk.X, pady=(0, 5))
    
    ttk.Label(search_frame, text="搜索:").pack(side=tk.LEFT)
    search_var = tk.StringVar()
    search_entry = ttk.Entry(search_frame, textvariable=search_var)
    search_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
    
    # 文件列表
    file_list_frame = ttk.Frame(left_frame)
    file_list_frame.pack(fill=tk.BOTH, expand=True)
    
    file_listbox = tk.Listbox(file_list_frame, font=("Arial", 10))
    file_listbox.pack(fill=tk.BOTH, expand=True)
    
    # 文件状态标签
    file_status_label = ttk.Label(left_frame, text="离线模式")
    file_status_label.pack(fill=tk.X, pady=(5, 0))
    
    # 右侧编辑框架
    right_frame = ttk.Frame(main_frame)
    right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
    
    # 标题编辑
    ttk.Label(right_frame, text="标题:").pack(anchor=tk.W, pady=(0, 5))
    title_var = tk.StringVar()
    title_entry = ttk.Entry(right_frame, textvariable=title_var, font=("Arial", 12))
    title_entry.pack(fill=tk.X, pady=(0, 10))
    
    # 内容编辑
    ttk.Label(right_frame, text="内容:").pack(anchor=tk.W, pady=(0, 5))
    content_text = scrolledtext.ScrolledText(
        right_frame, 
        wrap=tk.WORD, 
        font=("Arial", 11),
        undo=True
    )
    content_text.pack(fill=tk.BOTH, expand=True)
    
    # 状态栏
    status_var = tk.StringVar(value="就绪")
    status_bar = ttk.Label(root, textvariable=status_var, relief=tk.SUNKEN)
    status_bar.pack(side=tk.BOTTOM, fill=tk.X)
    
    # 右键菜单
    context_menu = tk.Menu(root, tearoff=0)
    
    return {
        "search_var": search_var,
        "search_entry": search_entry,
        "file_listbox": file_listbox,
        "file_status_label": file_status_label,
        "title_var": title_var,
        "title_entry": title_entry,
        "content_text": content_text,
        "status_var": status_var,
        "context_menu": context_menu
    }

def create_login_dialog(root, config, note_manager, update_account_menu_callback, 
                       file_status_label, sync_files_callback):
    """创建登录对话框"""
    login_window = tk.Toplevel(root)
    login_window.title("WebDAV 登录")
    login_window.geometry("400x200")
    login_window.transient(root)
    login_window.grab_set()
    
    ttk.Label(login_window, text="邮箱:").pack(pady=(20, 5))
    email_var = tk.StringVar(value=config.email)
    email_entry = ttk.Entry(login_window, textvariable=email_var, width=40)
    email_entry.pack(pady=5)
    
    ttk.Label(login_window, text="密码:").pack(pady=5)
    password_var = tk.StringVar(value=config.password)
    password_entry = ttk.Entry(login_window, textvariable=password_var, width=40, show="*")
    password_entry.pack(pady=5)
    
    ttk.Label(login_window, text="WebDAV服务器地址 (可选):").pack(pady=5)
    webdav_var = tk.StringVar(value=config.webdav_url)
    webdav_entry = ttk.Entry(login_window, textvariable=webdav_var, width=40)
    webdav_entry.pack(pady=5)
    
    def do_login():
        email = email_var.get().strip()
        password = password_var.get().strip()
        webdav_url = webdav_var.get().strip()
        
        if not email or not password:
            tk.messagebox.showwarning("警告", "请输入邮箱和密码")
            return
        
        if not webdav_url:
            webdav_url = config.default_webdav_url
        
        def auth_thread():
            success = note_manager.authenticate(email, password, webdav_url)
            login_window.after(0, lambda: on_auth_complete(success))
        
        import threading
        threading.Thread(target=auth_thread, daemon=True).start()
    
    def on_auth_complete(success):
        if success:
            login_window.destroy()
            update_account_menu_callback()
            file_status_label.config(text="已登录")
            sync_files_callback()
        else:
            tk.messagebox.showerror("错误", "认证失败，请检查邮箱和密码")
    
    ttk.Button(login_window, text="登录", command=do_login).pack(pady=10)
    
    # 绑定回车键
    login_window.bind('<Return>', lambda e: do_login())
    email_entry.focus()
    
    return login_window